package com.hongxun.design.adapters;

import android.app.Activity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import java.util.List;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.activitys.EditImageActivity;
import com.hongxun.design.bean.PhotoEnvironmentalItemBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.utils.InputUtil;
import com.hongxun.design.utils.PopwindowUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;
import com.hongxun.design.utils.soundUtil.UPlayer;

/**
 * Created by zerdoor_pc on 2016/4/29.
 */
public class PEnvironmentalAdapter extends BaseAdapter {

    private List<PhotoEnvironmentalItemBean> beanList;
    LayoutInflater inflater;
    Activity context;
    /**
     * 语音相关*
     */
    public SpeechHolder speechHolder;
    public int mPosition = -1;

    public PEnvironmentalAdapter(List<PhotoEnvironmentalItemBean> beanList, Activity context) {
        this.beanList = beanList;
        this.context = context;
        inflater = LayoutInflater.from(context);
        MyLog.L("HTTP_SUCCESSED：beanList=" + JSON.toJSONString(beanList));
    }

    public List<PhotoEnvironmentalItemBean> getData() {
        return beanList;
    }

    public void setBeanList(List<PhotoEnvironmentalItemBean> beanList) {
        this.beanList = beanList;
        notifyDataSetChanged();
    }

    public void update() {
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getItemViewType(int position) {
        int type = Constant.EdItem;
        switch (beanList.get(position).getType()) {
            case Constant.EdItem:
                type = Constant.EdItem;
                break;
            case Constant.ChoiceItem:
                type = Constant.ChoiceItem;
                break;
            case Constant.remarksItem:
                type = Constant.remarksItem;
                break;
            case Constant.SpeechItem:
                type = Constant.SpeechItem;
                break;
        }
        return type;
    }

    private int index = -1;

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        int ViewType = getItemViewType(position);
        EditTextHolder editTextHolder = null;
        ChoiceHolder choiceHolder = null;
        RemarksHolder remarksHolder = null;
        SpeechHolder speechHolder = null;

        switch (ViewType) {
            case Constant.EdItem:
                convertView = inflater.inflate(R.layout.listview_item_photo_ed, null);
                editTextHolder = new EditTextHolder(convertView);
                convertView.setTag(editTextHolder);
                break;
            case Constant.ChoiceItem:
                convertView = inflater.inflate(R.layout.listview_item_photo_choice, null);
                choiceHolder = new ChoiceHolder(convertView);
                convertView.setTag(choiceHolder);
                break;
            case Constant.remarksItem:
                convertView = inflater.inflate(R.layout.listview_item_photo_remarks, null);
                remarksHolder = new RemarksHolder(convertView);
                convertView.setTag(remarksHolder);
                break;
            case Constant.SpeechItem:
                convertView = inflater.inflate(R.layout.listview_item_photo_speech, null);
                speechHolder = new SpeechHolder(convertView);
                convertView.setTag(speechHolder);
                break;
        }

//        } else {
//            switch (ViewType) {
//                case Constant.EdItem:
//
//                    editTextHolder = (EditTextHolder) convertView.getTag();
//
//                    break;
//                case Constant.ChoiceItem:
//
//                    choiceHolder = (ChoiceHolder) convertView.getTag();
//
//                    break;
//                case Constant.remarksItem:
//
//                    remarksHolder = (RemarksHolder) convertView.getTag();
//                    break;
//                case Constant.SpeechItem:
//                    speechHolder = (SpeechHolder) convertView.getTag();
//                    break;
//            }
//        }


        try {
            PhotoEnvironmentalItemBean bean;
            EditImageActivity.btn_vocie = false;
            switch (ViewType) {
                case Constant.EdItem:
                    bean = beanList.get(position);
                    editTextHolder.photo_ed_title.setText(bean.getTitle());
                    editTextHolder.photo_ed_write.addTextChangedListener(bean.getTextWatcher());
                    if (bean.getValue().length() == 0) {
                        editTextHolder.photo_ed_write.setHint(bean.getHintValue());
                    } else {
                        editTextHolder.photo_ed_write.setText(bean.getValue());
                    }

                    final int p = position;
                    editTextHolder.photo_ed_write.setOnTouchListener(new MOnTouchListener(bean,editTextHolder) {
                        @Override
                        public boolean onTouch(View view, MotionEvent event) {
                            PhotoEnvironmentalItemBean bean= (PhotoEnvironmentalItemBean) msg;
                            EditTextHolder editTextHolder= (EditTextHolder) holldler;
                            if (event.getAction() == MotionEvent.ACTION_UP) {
                                index = p;
                                ((EditText)view).setText("");
                                bean.setValue("");
                            }

                            return false;
                        }
                    });
                    editTextHolder.photo_ed_write.clearFocus();
                    if (index != -1 && index == position) {
                        // 如果当前的行下标和点击事件中保存的index一致，手动为EditText设置焦点。

                        editTextHolder.photo_ed_write.requestFocus();
                    }
                    break;
                case Constant.ChoiceItem:
                    // choiceHolder
                    bean = beanList.get(position);
                    choiceHolder.title.setText(bean.getTitle());
                    choiceHolder.value.setText(bean.getValue());
                    choiceHolder.item.setOnClickListener(new MyOnClickListener(bean, choiceHolder.value) {
                        @Override
                        public void onClick(View view, PhotoEnvironmentalItemBean bean, TextView textView) {
                            InputUtil.closeInput(context);
                            WindowManager.LayoutParams params = context.getWindow().getAttributes();
                            params.alpha = 0.7f;
                            context.getWindow().setAttributes(params);
                            PopwindowUtil.popChoice_photo(context, textView, bean);
                        }
                    });
                    break;
                case Constant.remarksItem:
                    bean = beanList.get(position);
                    remarksHolder.photo_remarks.addTextChangedListener(bean.getTextWatcher());
                    if (bean.getValue().length() == 0) {
                        remarksHolder.photo_remarks.setHint(bean.getHintValue());
                    } else {
                        remarksHolder.photo_remarks.setText(bean.getValue());
                    }
                    final int p2 = position;
                    remarksHolder.photo_remarks.setOnTouchListener(new View.OnTouchListener() {
                        public boolean onTouch(View view, MotionEvent event) {
                            if (event.getAction() == MotionEvent.ACTION_UP) {
                                index = p2;
                            }
                            return false;
                        }
                    });
                    remarksHolder.photo_remarks.clearFocus();
                    if (index != -1 && index == position) {
                        // 如果当前的行下标和点击事件中保存的index一致，手动为EditText设置焦点。
                        remarksHolder.photo_remarks.requestFocus();
                    }
                    break;
                case Constant.SpeechItem:

                    bean = beanList.get(position);
                    setSpeech(bean, speechHolder, position);
                    break;
            }
            return convertView;

        } catch (Exception e) {
            return null;
        }


    }

//    public int mPosition = -1;
//    public SpeechHolder speechHolder;

    public void setSpeech(PhotoEnvironmentalItemBean bean, SpeechHolder speechHolder, int position) {


        /**设置播放按钮样式**/
        SetTypeface.set(speechHolder.speech_return_button, IconSourse.Recording);//设置对应字体与文字
        SpannableString styledText1 = new SpannableString(" 重录");
        styledText1.setSpan(new TextAppearanceSpan(context, R.style.style0), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        speechHolder.speech_return_button.append(styledText1);
        SetTypeface.set(speechHolder.speech_play, IconSourse.PLAY);//设置对应字体与文字
        SpannableString styledText;
        if (bean.getValue().length() == 0) {
            styledText = new SpannableString(" 播放");
            styledText.setSpan(new TextAppearanceSpan(context, R.style.style0), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else {
            String value = bean.getValue();
            styledText = new SpannableString(value);
            styledText.setSpan(new TextAppearanceSpan(context, R.style.style0), 0, value.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }


        speechHolder.speech_play.append(styledText);
        /**设置录音按钮样式**/
        SetTypeface.set(speechHolder.speech_button, IconSourse.Recording);//设置对应字体与文字
        SpannableString styledText2 = new SpannableString(" 语音备注");
        styledText2.setSpan(new TextAppearanceSpan(context, R.style.style0), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        speechHolder.speech_button.append(styledText2);
        if (bean.getPath() != null && bean.getPath().length() > 0) {
            speechHolder.speech_play_button.setVisibility(View.VISIBLE);//显示播放样式
            speechHolder.speech_button.setVisibility(View.GONE);//隐藏录音样式
           // double t =(double) UPlayer.getIntence(bean.getPath()).getDuration() / (double) 1000;
           // HttpStringUtil.getPath(bean.getPath());

            String time = "0.00";
            if (bean.getPath() != null && bean.getPath().length() != 0) {
                time = ((double) UPlayer.getIntence(bean.getPath()).getDuration() / (double) 1000) + "";
            }
            if (time==null||time.length()==0)time= MString.play;

            bean.setValue(time + "");
        } else {
            speechHolder.speech_play_button.setVisibility(View.GONE);//
            speechHolder.speech_button.setVisibility(View.VISIBLE);//

        }
        this.speechHolder = speechHolder;
        mPosition = position;
        /**设置录音按钮触摸事件**/
        speechHolder.speech_button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (!MyApp.ReadOnly) {
                    MyLog.L(MyApp.AppName + "进入：" + motionEvent);
                    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                        MyLog.L(MString.SUCCESSED + JSON.toJSONString(beanList));
                        EditImageActivity.btn_vocie = true;
                    } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                        EditImageActivity.btn_vocie = false;
                    }
                    return false;
                }else{
                    return true;
                }
            }
        });
        speechHolder.speech_return_button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (!MyApp.ReadOnly) {
                    MyLog.L(MyApp.AppName + "进入：" + motionEvent);
                    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                        MyLog.L(MString.SUCCESSED + JSON.toJSONString(beanList));
                        EditImageActivity.btn_vocie = true;
                    } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                        EditImageActivity.btn_vocie = false;
                    }
                    return false;
                }else{
                    return true;
                }
            }
        });

        /**设置重录按钮事件**/
//        speechHolder.speech_return_button.setOnClickListener(new MyClickListener(speechHolder, bean) {
//            @Override
//            public void onClick(View view, SpeechHolder speechHolder, PhotoEnvironmentalItemBean bean) {
//                if (!MyApp.ReadOnly) {
//                    UPlayer.getIntence(bean.getPath()).stop();
//                    speechHolder.speech_button.setVisibility(View.VISIBLE);
//                    speechHolder.speech_play_button.setVisibility(View.GONE);
//                    bean.setPath("");
//                }
//            }
//        });
        /**设置语音播放按钮事件**/
        speechHolder.speech_play.setOnClickListener(new MyClickListener(speechHolder, bean) {


            @Override
            public void onClick(View view, SpeechHolder speechHolder, PhotoEnvironmentalItemBean bean) {
                if (bean.getPath() != null && bean.getPath().length() > 0) {
                    UPlayer.getIntence(bean.getPath()).start();
                } else {
                    if (!MyApp.ReadOnly) {
                        speechHolder.speech_button.setVisibility(View.VISIBLE);
                        speechHolder.speech_play_button.setVisibility(View.GONE);
                    }
                }
            }
        });
    }


    class EditTextHolder {
        public TextView photo_ed_title, photo_ed_right;
        public EditText photo_ed_write;

        EditTextHolder(View convertView) {
            photo_ed_title = (TextView) convertView.findViewById(R.id.photo_ed_title);
            photo_ed_write = (EditText) convertView.findViewById(R.id.photo_ed_write);
            photo_ed_right = (TextView) convertView.findViewById(R.id.photo_ed_right);

        }
    }

    class ChoiceHolder {
        public TextView title, value, right;
        RelativeLayout item;

        ChoiceHolder(View convertView) {
            item = (RelativeLayout) convertView.findViewById(R.id.photo_choice_item);
            title = (TextView) convertView.findViewById(R.id.photo_choice_title);
            value = (TextView) convertView.findViewById(R.id.photo_choice_value);
            right = (TextView) convertView.findViewById(R.id.photo_choice_right);
            SetTypeface.set(right, IconSourse.DROPDOWN);
        }
    }


    class RemarksHolder {
        public EditText photo_remarks;

        RemarksHolder(View convertView) {
            photo_remarks = (EditText) convertView.findViewById(R.id.photo_remarks);
        }
    }

    public class SpeechHolder {
        public TextView speech_button, speech_return_button, speech_play;
        public LinearLayout speech_play_button;

        SpeechHolder(View convertView) {
            speech_button = (TextView) convertView.findViewById(R.id.photo_speech_button);//录音按钮
            speech_play_button = (LinearLayout) convertView.findViewById(R.id.photo_speech_play_button);//播放样式
            speech_return_button = (TextView) convertView.findViewById(R.id.photo_speech_return_button);//返回重录按钮
            speech_play = (TextView) convertView.findViewById(R.id.photo_speech_play);//播放按钮
        }
    }


    abstract class MOnTouchListener implements View.OnTouchListener{
        public Object msg,holldler;
        MOnTouchListener(Object bean,Object holldler){
                this.msg=bean;
                this.holldler=holldler;
        }
    }
    abstract class MyOnClickListener implements View.OnClickListener {

        private PhotoEnvironmentalItemBean bean;
        TextView textView;

        MyOnClickListener(PhotoEnvironmentalItemBean bean, TextView textView) {
            this.textView = textView;
            this.bean = bean;
        }

        @Override
        public void onClick(View view) {
            onClick(view, bean, textView);
        }

        public abstract void onClick(View view, PhotoEnvironmentalItemBean bean, TextView textView);
    }

    abstract class MyClickListener implements View.OnClickListener {
        private SpeechHolder speechHolder;
        private PhotoEnvironmentalItemBean bean;

        MyClickListener(SpeechHolder speechHolder, PhotoEnvironmentalItemBean bean) {
            this.speechHolder = speechHolder;
            this.bean = bean;
        }

        @Override
        public void onClick(View view) {
            onClick(view, speechHolder, bean);
        }

        public abstract void onClick(View view, SpeechHolder speechHolder, PhotoEnvironmentalItemBean bean);
    }
}
